/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xy.dto.ExhibitionPositionDTO;
import com.xy.entity.*;
import com.xy.service.*;
import com.xy.vo.ExhibitionPositionVO;
import com.xy.excel.ExhibitionPositionExcel;
import com.xy.mapper.ExhibitionPositionMapper;
import com.xy.wrapper.ExhibitionPositionWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 展会与门禁点关联表 服务实现类
 *
 * @author BladeX
 * @since 2023-07-18
 */
@Service
public class ExhibitionPositionServiceImpl extends BaseServiceImpl<ExhibitionPositionMapper, ExhibitionPositionEntity> implements IExhibitionPositionService {

	@Autowired
	IPositionGroupService positionGroupService;
	@Lazy
	@Autowired
	IPositionService positionService;
	@Autowired
	IViewerSortService viewerTypeService;
	@Autowired
	IPositionGroupViewerTypeService IPositionGroupViewerTypeService;
	/**
	 * 是否为内馆0是1否
	 */
	@ApiModelProperty(value = "是否为内馆0是1否")
	private Integer groupInner;
	@Override
	public IPage<ExhibitionPositionVO> selectExhibitionPositionPage(IPage<ExhibitionPositionVO> page, ExhibitionPositionVO exhibitionPosition) {
		return page.setRecords(baseMapper.selectExhibitionPositionPage(page, exhibitionPosition));
	}

	@Override
	public IPage<ExhibitionPositionVO> listPage(ExhibitionPositionDTO exhibitionPosition, Query query) {
		IPage<ExhibitionPositionEntity> pages = this.page(Condition.getPage(query), Condition.getQueryWrapper(exhibitionPosition));
		IPage<ExhibitionPositionVO> positionVOIPage = ExhibitionPositionWrapper.build().pageVO(pages);
		List<ExhibitionPositionVO> entityList = positionVOIPage.getRecords();
		List<PositionGroupEntity> positionGroupEntityList = positionGroupService.list();
		Map<Long, PositionGroupEntity> pointGroupEntityMap = positionGroupEntityList.stream().collect(Collectors.toMap(PositionGroupEntity::getId, o -> o));
		List<PositionEntity> positionEntityList = positionService.list();
		Map<Long, PositionEntity> positionEntityMap = positionEntityList.stream().collect(Collectors.toMap(PositionEntity::getId, o -> o));
		entityList.forEach(o->{
			PositionEntity position = positionEntityMap.get(o.getPositionId());
			PositionGroupEntity groupEntity = pointGroupEntityMap.get(o.getPositionGroupId());
			o.setGroupName(groupEntity.getGroupName());
			o.setPositionName(position.getPositionName());
		});
		positionVOIPage.setRecords(entityList);
		return positionVOIPage;
	}

	@Override
	public IPage<ExhibitionPositionVO> listGroupType(ExhibitionPositionDTO exhibitionProint, Query query) {

		LambdaQueryWrapper<ExhibitionPositionEntity> queryWrapper = Wrappers.lambdaQuery(exhibitionProint);
		queryWrapper.groupBy(ExhibitionPositionEntity::getPositionGroupId);
		IPage<ExhibitionPositionEntity> pages = this.page(Condition.getPage(query),queryWrapper);
		IPage<ExhibitionPositionVO> prointVOIPage = ExhibitionPositionWrapper.build().pageVO(pages);
		List<ExhibitionPositionVO> entityList = prointVOIPage.getRecords();

		entityList.forEach(o->{
			PositionGroupEntity groupEntity = positionGroupService.getById(o.getPositionGroupId());
			o.setGroupPhoto(groupEntity.getGroupPhoto());
			o.setGroupName(groupEntity.getGroupName());
			o.setGroupInner(groupEntity.getGroupInner());
			LambdaQueryWrapper<PositionGroupViewerTypeEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
			lambdaQueryWrapper.eq(PositionGroupViewerTypeEntity::getExhibitionId,exhibitionProint.getExhibitionId());
			lambdaQueryWrapper.eq(PositionGroupViewerTypeEntity::getPositionGroupId,o.getPositionGroupId());
			List<PositionGroupViewerTypeEntity> typeEntityList = IPositionGroupViewerTypeService.list(lambdaQueryWrapper);
			if(CollectionUtil.isNotEmpty(typeEntityList)){
				List<Long> longList = typeEntityList.stream().map(PositionGroupViewerTypeEntity::getViewerTypeId).collect(Collectors.toList());
				List<ViewerSortEntity> list = viewerTypeService.listByIds(longList);
				o.setTypeList(list);
				o.setFrontBack(typeEntityList.stream().findFirst().get().getFrontBack());
			}
		});
		prointVOIPage.setRecords(entityList);
		return prointVOIPage;
	}


	@Override
	public List<ExhibitionPositionExcel> exportExhibitionPosition(Wrapper<ExhibitionPositionEntity> queryWrapper) {
		List<ExhibitionPositionExcel> exhibitionPositionList = baseMapper.exportExhibitionPosition(queryWrapper);
		//exhibitionPositionList.forEach(exhibitionPosition -> {
		//	exhibitionPosition.setTypeName(DictCache.getValue(DictEnum.YES_NO, ExhibitionPosition.getType()));
		//});
		return exhibitionPositionList;
	}

}
